package com.tywj.buscustomerapp.view.item;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.BusTravelBean;
import com.tywj.buscustomerapp.model.bean.RequesrDaysTicketBean;
import com.tywj.buscustomerapp.model.bean.TravelLineBean;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.TaylorClickLinstener;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;
import com.tywj.buscustomerapp.view.activity.BuyTicketsActivity;
import com.tywj.buscustomerapp.view.activity.LoginActivity;
import com.tywj.buscustomerapp.view.activity.MapActivity;
import com.tywj.buscustomerapp.view.activity.PayActivity;
import com.tywj.buscustomerapp.view.activity.newBuyTicketActivity;
import com.tywj.buscustomerapp.view.myview.ToastJoe;

import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BusLineItem implements AdapterItem<BusTravelBean.DataBean.ResultListBean> {

    private TextView tag;
    private TextView lineName;
    private TextView lineTime;
    private TextView lineInfo;
    private TextView lineButton;
    private ConstraintLayout travel;
    private Context context;
//    private ImageView line_travel_img;
    private TextView line_time1;
    private int position = 0;

    @Override
    public int getLayoutResId() {
        return R.layout.item_travel_line1;
//        if (position==0){
//            return R.layout.item_travel_line;
//        }else{
//            return R.layout.item_travel_line1;
//        }
    }

    @Override
    public void bindViews(@NonNull View root) {
        context = root.getContext();
        tag = root.findViewById(R.id.line_tag);
        lineName = root.findViewById(R.id.line_travel);
        lineTime = root.findViewById(R.id.line_time);
        lineButton = root.findViewById(R.id.line_button);
        lineInfo = root.findViewById(R.id.line_info);
        travel = root.findViewById(R.id.travel);
        line_time1 = root.findViewById(R.id.line_time1);
//        line_travel_img = root.findViewById(R.id.line_travel_img);
    }

    @Override
    public void setViews() {

    }

    @Override
    public void handleData(final BusTravelBean.DataBean.ResultListBean busLineBean, int position) {
        this.position = position;
        if (busLineBean!=null){
            lineName.setText(busLineBean.getXlmc());
            lineInfo.setText(busLineBean.getXlqsz()+"——"+busLineBean.getXlzdz());
            lineTime.setText(busLineBean.getFcsj()+"——"+busLineBean.getDdsj());
            lineButton.setText("￥"+busLineBean.getXlpj()+" 购票");
            if (busLineBean.getType().equals("CommuteLine")){
                if (busLineBean.getFcsj()!=null && Integer.valueOf(busLineBean.getFcsj().substring(0,2))>12){
                    line_time1.setVisibility(View.GONE);
                }else{
                    line_time1.setVisibility(View.VISIBLE);
                }
                tag.setVisibility(View.VISIBLE);
            }else{
                line_time1.setVisibility(View.GONE);
                tag.setVisibility(View.GONE);
            }
            lineButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String phone = SPUtils.getUserPhone(v.getContext().getApplicationContext());
                    if (phone != null && !phone.equals("")) {
                        Intent intent = new Intent(v.getContext(), newBuyTicketActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("bus", busLineBean);
                        intent.putExtras(bundle);
                        v.getContext().startActivity(intent);
                    } else {
                        Intent intent = new Intent(v.getContext(), LoginActivity.class);
                        v.getContext().startActivity(intent);
                    }
                }
            });
            travel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), MapActivity.class);
                    intent.putExtra("lineNum", busLineBean);
                    v.getContext().startActivity(intent);
                }
            });
        }
    }
}
